-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
hlb = require('libhlb')
printf = require "mod_print"

function device_open()
	local ret = hlb.usb_open(0x6666F0F2)
	if ret < 0 then
		print("Fail to open usb device")
		return false
	end
	
	return true
end

function device_close()
	local ret = hlb.usb_close()
	if ret < 0 then
		print("Fail to close usb device")
		return false
	end
	
	return true
end

function pause()
    io.write("\r\nAny key to continue ...")
    io.flush()
    io.read()
    print("")
end
-- -----------------------------------------------
function send_ctrl_cmd(cmd_data)
    if type(cmd_data) ~= "table" then
        print("cmd_data must be table type")
        return false
    end

    local msg = {0x40, 0xb0, 0, 0, #cmd_data, 100}
    local ret

    if #cmd_data > 0 then
        ret = hlb.usb_control_msg(cmd_data, msg)
    else
        ret = hlb.usb_control_msg(msg)
    end
    
    if ret < 0 then
        print("send_ctrl_cmd() ret = " .. ret)
        return false
    end

    return true
end
-- -----------------------------------------------
function spi_command(data_table)
    local cmd = {}
    cmd[1] = 0x30
    cmd[2] = #data_table
    for i=1, #data_table do
        cmd[i+2] = data_table[i]
    end

    --printf.table(cmd, true)

    local ret = hlb.usb_bulk_write(0x01, cmd, 1000)
    if type(ret) ~= "number" then
        print("Error: usb_bulk_write ret type = " .. type(ret))
        return -1
    end

    ret = hlb.usb_bulk_read(0x81, #cmd, 1000)
    --if type(ret) == "number" then
    --    print("usb_bulk_read ret = " .. ret)
    --end

    return ret
end
-- -----------------------------------------------
function write_then_read(address, data)
    local cmd_data = {};
    local cmd_addr = {};
    local ret;

    cmd_addr[1] = bit32.band(bit32.rshift(address, 16), 0xFF) + 0x80
    cmd_addr[2] = bit32.band(bit32.rshift(address, 8), 0xFF)
    cmd_addr[3] = bit32.band(address, 0xFF)

    cmd_data[1] = 0x60 + #data;
    for i=1, #data do
        cmd_data[i + 1] = data[i]
    end

    ret = spi_command(cmd_addr)
    if type(ret) == "number" then
        print("Error : spi_command ret = " .. ret)
        return -1
    end

    ret = spi_command(cmd_data)
    if type(ret) == "number" then
        print("Error : spi_command ret = " .. ret)
        return -2
    end

    ret = spi_command(cmd_addr)
    if type(ret) == "number" then
        print("Error : spi_command ret = " .. ret)
        return -3
    end

    cmd_data[1] = 0x40 + #data;
    ret = spi_command(cmd_data)
    if type(ret) == "number" then
        print("Error : spi_command ret = " .. ret)
        return -4
    end
    
    -- remove command head
    for i=1, #ret do
        if i <= #ret - 3 then
            ret[i] = ret[i+3]
        else
            ret[i] = nil
        end
    end

    return ret
end
-- -----------------------------------------------
ret = device_open()
if ret == false then
    print("device_open fail")
    return
end

local cmd = { 0x00, 0x00, 0x00, 0x01}--enable steppers
ret = spi_command(cmd)
if type(ret) == "number" then
	print("Error : spi_command ret = " .. ret)
	return
end
printf.table(ret, true)

cmd = { 0x03, 0x00, 0x00, 50}--set tick top
ret = spi_command(cmd)
if type(ret) == "number" then
	print("Error : spi_command ret = " .. ret)
	return
end
printf.table(ret, true)

cmd = { 0x04, 0x00, 0x02, 0}--set x step num
ret = spi_command(cmd)
if type(ret) == "number" then
	print("Error : spi_command ret = " .. ret)
	return
end
printf.table(ret, true)

cmd = { 0x05, 0x00, 0x33, 0x20}--set x step ticks
ret = spi_command(cmd)
if type(ret) == "number" then
	print("Error : spi_command ret = " .. ret)
	return
end
printf.table(ret, true)

cmd = { 0x06, 0x00, 0x00, 13}--set y step num
ret = spi_command(cmd)
if type(ret) == "number" then
	print("Error : spi_command ret = " .. ret)
	return
end
printf.table(ret, true)

cmd = { 0x07, 0x00, 0xf0, 0x00}--set y step ticks
ret = spi_command(cmd)
if type(ret) == "number" then
	print("Error : spi_command ret = " .. ret)
	return
end
printf.table(ret, true)

cmd = { 0x02, 0x00, 0x00, 0x32}--run  stepper
ret = spi_command(cmd)
if type(ret) == "number" then
	print("Error : spi_command ret = " .. ret)
	return
end
printf.table(ret, true)


device_close()
